package com.example.myapplication;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

public class DiscoverFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ScrollView scrollView = new ScrollView(getContext());
        scrollView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        scrollView.setBackgroundColor(0xFFF5F5F5); // #f5f5f5
        scrollView.setPadding(48, 48, 48, 48); // 12dp * 4 = 48px (假设屏幕密度为mdpi)


        LinearLayout mainLayout = new LinearLayout(getContext());
        mainLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        scrollView.addView(mainLayout);


        LinearLayout functionContainer = createFunctionGrid();
        mainLayout.addView(functionContainer);


        TextView recommendTitle = new TextView(getContext());
        recommendTitle.setText("推荐内容");
        recommendTitle.setTextSize(18);
        recommendTitle.setTextColor(0xFF333333); // #333333
        recommendTitle.setTypeface(null, android.graphics.Typeface.BOLD);

        LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        titleParams.setMargins(0, 64, 0, 32); // 16dp top, 8dp bottom
        recommendTitle.setLayoutParams(titleParams);

        mainLayout.addView(recommendTitle);


        LinearLayout recommendContainer = createRecommendContent();
        mainLayout.addView(recommendContainer);

        return scrollView;
    }

    private LinearLayout createFunctionGrid() {

        LinearLayout container = new LinearLayout(getContext());
        container.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        container.setOrientation(LinearLayout.VERTICAL);
        container.setBackgroundColor(0xFFFFFFFF); // 白色背景
        container.setPadding(32, 32, 32, 32); // 8dp * 4 = 32px


        container.addView(createFunctionRow(new String[]{"朋友圈", "视频号", "直播"}));


        View divider1 = new View(getContext());
        divider1.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                4)); // 1dp
        divider1.setBackgroundColor(0xFFE0E0E0); // #E0E0E0
        container.addView(divider1);


        container.addView(createFunctionRow(new String[]{"扫一扫", "摇一摇", "看一看"}));

        return container;
    }

    private LinearLayout createFunctionRow(String[] items) {
        LinearLayout rowLayout = new LinearLayout(getContext());
        rowLayout.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        rowLayout.setOrientation(LinearLayout.HORIZONTAL);
        rowLayout.setPadding(0, 32, 0, 32); // 8dp top and bottom

        for (String item : items) {
            TextView textView = new TextView(getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    0,
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    1.0f);
            textView.setLayoutParams(params);
            textView.setText(item);
            textView.setTextSize(16);
            textView.setGravity(Gravity.CENTER);
            rowLayout.addView(textView);
        }

        return rowLayout;
    }

    private LinearLayout createRecommendContent() {

        LinearLayout container = new LinearLayout(getContext());
        container.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        container.setOrientation(LinearLayout.VERTICAL);
        container.setBackgroundColor(0xFFFFFFFF); // 白色背景
        container.setPadding(32, 32, 32, 32); // 8dp * 4 = 32px


        container.addView(createRecommendItem("推荐内容 1"));
        container.addView(createRecommendItem("推荐内容 2"));
        container.addView(createRecommendItem("推荐内容 3"));

        return container;
    }

    private TextView createRecommendItem(String text) {
        TextView textView = new TextView(getContext());
        textView.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        textView.setText(text);
        textView.setTextSize(16);
        textView.setPadding(32, 32, 32, 32); // 8dp padding
        return textView;
    }
}